package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import service.MenuService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 包名:  com.iteitan.service.impl;
 * 作者:  Eitan
 * 日期:  2019/9/9 10:18
 */
@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    @Override
    public List<Map> findParentMenu() {
        List<Menu> list = menuDao.findParentMenu();
        List<Map> resMap=new ArrayList<>();
        for (Menu menu : list) {
            Map<String,Object> mapList = new HashMap<>();
            mapList.put("value",menu.getId());
            mapList.put("label",menu.getName());
            resMap.add(mapList);
        }
        return resMap;
    }

    /**
     * 添加菜单
     * @param menu
     */
    @Override
    public void add(Menu menu) {
        Integer parentMenuId = menu.getParentMenuId();
        //处理访问路径字段
        if(parentMenuId==null){
            int pathMax =menuDao.findMaxPath();
            menu.setPath(pathMax+1+"");
        }else {
            String pathStr = "/"+menu.getParentMenuId().toString()+"-"+menu.getPriority();
            menu.setPath(pathStr);
        }
        menuDao.add(menu);
    }

    /**
     * 菜单分页
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Menu> page= menuDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 删除菜单
     * @param id
     */
    @Override
    public void delete(Integer id) throws RuntimeException{
        //删除之前判断是否还有下级菜单
        int count = menuDao.findChildById(id);
        if(count > 0){
            throw new RuntimeException("当前菜单还有子菜单,不能删除,无效操作");
        }
        //判断是否还与角色关联
        int roleIds=menuDao.findMenuRoleByMenuId(id);
        if(roleIds > 0){
            throw new RuntimeException("当前菜单与角色绑定,不能删除,无效操作");
        }
        menuDao.deleteById(id);
    }

    /**
     * 查询所有菜单数据
     * @return
     */
    @Override
    public List<Menu> findMenuAll() {
        List<Menu> menuList = menuDao.findMenuAll();
        return menuList;
    }

    /**
     * 根据id查找菜单
     * @param id
     * @return
     */
    @Override
    public Menu findByMenuId(Integer id) {
        return menuDao.findByMenuId(id);
    }

    /**
     * 编辑菜单
     * @param menu
     */
    @Override
    public void edit(Menu menu) {
        menuDao.edit(menu);
    }

    /**
     * 根据角色id查询角色的菜单访问
     * @param id
     * @return
     */
    @Override
    public List<Integer> findMenuByRoleId(int id) {
        return menuDao.findMenuByRoleId(id);
    }


}
